<!--
 * @Author: JPeng 872639397@qq.com
 * @Date: 2022-05-14 23:00:10
 * @LastEditors: JPeng 872639397@qq.com
 * @LastEditTime: 2022-05-14 23:00:14
 * @FilePath: \review-review-review\复习！！！\Promise\race.html
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        let p1 = new Promise((resolve, reject) => {
            setTimeout(() => {
                resolve('success');
            }, 10000);
        });

        let p2 = new Promise((resolve, reject) => {
            setTimeout(() => {
                reject('faild');
            }, 500);
        });

        Promise.race([p1, p2]).then(res => {
            console.log(res);
        }).catch(err => {
            console.log(err); // 返回的是faild
        })

        // Promise.race()意为赛跑的意思，也就是数组中的任务哪个获取的块，就返回哪个，不管结果本身是成功
        // 还是失败。一般用于和定时器绑定，比如将一个请求和三秒的定时器包装成Promise实例，
        // 加入到Promise队列中，请求三秒中还没有回应时，给用户一些提示或相应的操作。

    </script>
</body>

</html>